/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.helper;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.DeleteProvider;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.annotations.UpdateProvider;

/**
 * Mapper 注解帮助类
 * @author Administrator
 *
 */
public class AnntHelper {
	public final static Set<Class<? extends Annotation>> sqlAnnotationTypes = new HashSet<Class<? extends Annotation>>();
	public final static Set<Class<? extends Annotation>> sqlProviderAnnotationTypes = new HashSet<Class<? extends Annotation>>();

	static {
		sqlAnnotationTypes.add(Select.class);
		sqlAnnotationTypes.add(Insert.class);
		sqlAnnotationTypes.add(Update.class);
		sqlAnnotationTypes.add(Delete.class);

		sqlProviderAnnotationTypes.add(SelectProvider.class);
		sqlProviderAnnotationTypes.add(InsertProvider.class);
		sqlProviderAnnotationTypes.add(UpdateProvider.class);
		sqlProviderAnnotationTypes.add(DeleteProvider.class);
	}

	/**
	 * 选择 Result 注解
	 * @param method
	 * @return
	 */
	public static Class<? extends Annotation> getResultAnnotationType(Method method) {
		return findAnnotationType(method, Result.class);
	}

	/**
	 * 选择 SelectKey 注解
	 * @param method
	 * @return
	 */
	public static Class<? extends Annotation> getSelectKeyAnnotationType(Method method) {
		return findAnnotationType(method, SelectKey.class);
	}

	/**
	 * 查找 注解
	 * @param method
	 * @param anntClass
	 * @return
	 */
	public static Class<? extends Annotation> findAnnotationType(Method method, Class<? extends Annotation> anntClass) {
		if (method.isAnnotationPresent(anntClass)) return anntClass;
		return null;
	}

	/**
	 * 选择 Sql 注解
	 * @param method
	 * @return
	 */
	public static Class<? extends Annotation> getSqlAnnotationType(Method method) {
		return chooseAnnotationType(method, sqlAnnotationTypes);
	}

	/**
	 * 选择 SqlProvider 注解
	 * @param method
	 * @return
	 */
	public static Class<? extends Annotation> getSqlProviderAnnotationType(Method method) {
		return chooseAnnotationType(method, sqlProviderAnnotationTypes);
	}

	/**
	 * 选择注解
	 * @param method
	 * @param types
	 * @return
	 */
	public static Class<? extends Annotation> chooseAnnotationType(Method method, Set<Class<? extends Annotation>> types) {
		for (Class<? extends Annotation> type : types) {
			Annotation annotation = method.getAnnotation(type);
			if (annotation != null) {
				return type;
			}
		}
		return null;
	}
}
